package br.ufmg.ubicomp.droidguide.application;

import java.io.IOException;

import br.ufmg.ubicomp.droidguide.R;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConfigurationManager;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConstants;
import br.ufmg.ubicomp.droidguide.context.Route;
import br.ufmg.ubicomp.droidguide.eventservice.DroidGuideServerRequestService;
import br.ufmg.ubicomp.droidguide.eventservice.DroidGuideServerRequestServiceImpl;
import br.ufmg.ubicomp.droidguide.eventservice.NotificationList;
import br.ufmg.ubicomp.droidguide.eventservice.ServiceList;
import br.ufmg.ubicomp.droidguide.eventservice.notification.DGNotificationType;
import br.ufmg.ubicomp.droidguide.utils.AndroidUtils;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Main extends DroidGuideScreenActivity implements OnClickListener {

	ImageButton profile, context, search, maps, route,
	  services, activities, notifications, config, exit;  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		profile = (ImageButton) findViewById(R.id.profile);
		profile.setImageResource(R.drawable.profile2);
		profile.setOnClickListener(this);
		
		context = (ImageButton) findViewById(R.id.contextButton);
		context.setImageResource(R.drawable.profile);
		context.setOnClickListener(this);
		
		search = (ImageButton) findViewById(R.id.search);
		search.setImageResource(R.drawable.find);
		search.setOnClickListener(this);
		
		maps = (ImageButton) findViewById(R.id.maps);
		maps.setImageResource(R.drawable.map2);
		maps.setOnClickListener(this);
		
		route = (ImageButton) findViewById(R.id.route);
		route.setImageResource(R.drawable.map);
		route.setOnClickListener(this);
		
		services = (ImageButton) findViewById(R.id.services);
		services.setImageResource(R.drawable.services2);
		services.setOnClickListener(this);
		
		activities = (ImageButton) findViewById(R.id.activities);
		activities.setImageResource(R.drawable.services);
		activities.setOnClickListener(this);
		
		notifications = (ImageButton) findViewById(R.id.notifications);
		notifications.setImageResource(R.drawable.info);
		notifications.setOnClickListener(this);
		
		config = (ImageButton) findViewById(R.id.config);
		config.setImageResource(R.drawable.config);
		config.setOnClickListener(this);
		
		exit = (ImageButton) findViewById(R.id.exit);
		exit.setImageResource(R.drawable.logout);
		exit.setOnClickListener(this);
	}

	@Override
	public void onClick(View button) {		
    	Intent i = null;
		
		if (button.getId() == profile.getId())
		{
        	i = new Intent(this,Profile.class);
        	startActivity(i);
		} else if (button.getId() == context.getId())
		{
        	i = new Intent(this,HowAmIActivity.class);
        	startActivity(i);
		}else if (button.getId() == search.getId())
		{
        	i = new Intent(this,Map.class);
        	startActivity(i);
		}else if (button.getId() == maps.getId())
		{
        	i = new Intent(this,Map.class);
        	startActivity(i);
		}else if (button.getId() == route.getId())
		{
        	i = new Intent(this,Map.class);
        	startActivity(i);
		}else if (button.getId() == services.getId())
		{
        	i = new Intent(this,ServiceList.class);
        	startActivity(i);
		}else if (button.getId() == activities.getId())
		{
        	i = new Intent(this,ActivitiesMenuList.class);
        	startActivity(i);
		}else if (button.getId() == notifications.getId())
		{
        	i = new Intent(this,NotificationList.class);
        	startActivity(i);
		}else if (button.getId() == config.getId())
		{
        	i = new Intent(this,Config.class);
        	startActivity(i);
		}if (button.getId() == exit.getId())
		{
			executeLogout();
		} 
	}
	
	private void executeLogout(){
		DroidGuideServerRequestService service = new DroidGuideServerRequestServiceImpl();
		try {
			service.executeLogout(DroidGuideConfigurationManager.getInstance().getData(DroidGuideConstants.DROID_GUIDE_USER_KEY));
			AndroidUtils.showNotification(this, "Logout", "Logout was sucessful.", DGNotificationType.OUTGOING, "Logout");
        	finish();
        	AndroidUtils.startActivity(this, Login.class);
		} catch (IOException e) {
			e.printStackTrace();
			AndroidUtils.notifyError(e, this, "executeLogout");
		}
	}
	
}
